﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data;
using Microsoft.SharePoint.WebPartPages;

namespace ECTSUserMgmtExtension
{
    public class UserManagerExtensionPager : System.Web.UI.WebControls.WebParts.WebPart, IUserManagerFilter
    {
        #region IUserManagerFilter Members
        private string _selectedFilter = "(cn=a*)";
        public string SelectedFilter
        {
            get { return _selectedFilter; }
        }

        #endregion

        enum Alphabet { a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z };

        #region ConnectionProvider
        [ConnectionProvider("User Manager Filter Provider", "UserManagerFilterProvider")]
        public IUserManagerFilter GetFilterProviderInstance()
        {
            return this as IUserManagerFilter;
        }
        #endregion

        #region FilterFields
        private string m_filterFields;
        [WebBrowsable]
        [WebDisplayName("Filter Fields")]
        [WebDescription("A ';' delimited list of AD Fields")]
        [WebPartStorage(Storage.Shared)]
        [Personalizable(PersonalizationScope.Shared)]
        [Category("Configuration")]
        [DefaultValue("")]
        public string FilterFields
        {
            get { return m_filterFields; }
            set { m_filterFields = value; }
        }
        #endregion

        #region FilterDataTable
        private DataTable FilterDataTable
        {
            get
            {
                DataTable filterFields = new DataTable();
                filterFields.Columns.Add("DisplayText");
                filterFields.Columns.Add("ADField");

                if (!string.IsNullOrEmpty(FilterFields))
                {
                    char[] seperator = { ';' };
                    string[] splitFields = FilterFields.Split(seperator, StringSplitOptions.RemoveEmptyEntries);
                    foreach (string field in splitFields)
                    {
                        filterFields.Rows.Add(field, field);
                    }
                }
                else
                {
                    filterFields.Rows.Add("cn", "cn");
                }
                return filterFields;
            }
        }
        #endregion

        private DropDownList m_dropDownField;
        protected override void CreateChildControls()
        {
            for(Alphabet letter = Alphabet.a; letter <= Alphabet.z; ++letter)
            {
                LinkButton letterLink = new LinkButton();
                letterLink.Text = letter.ToString();
                letterLink.CommandArgument = letter.ToString();
                letterLink.Click += new EventHandler(HandleLetterLinkClick);
                Controls.Add(letterLink);

                if(letter != Alphabet.z)
                    Controls.Add(new LiteralControl(" | "));
            }

            Controls.Add(new LiteralControl(" "));

            m_dropDownField = new DropDownList();
            m_dropDownField.DataSource = FilterDataTable;
            m_dropDownField.DataTextField = "DisplayText";
            m_dropDownField.DataValueField = "ADField";
            m_dropDownField.DataBind();

            Controls.Add(m_dropDownField);
        }

        void HandleLetterLinkClick(object sender, EventArgs e)
        {
            LinkButton letterLink = sender as LinkButton;
            _selectedFilter = string.Format("({0}={1}*)", m_dropDownField.SelectedValue, letterLink.CommandArgument.ToString());
        }

    }
}
